﻿using System;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GNUGo.Net.WebApp.Entities;
using GNUGo.Net.WebApp;
using GNUGo.Net.WebApp.WsClasses;

namespace SuperGo.TestWebApp {
	[TestClass]
	public class BaseTest {
		protected static GoEntitiesDataContext _dataContext;

		protected static GoEntitiesDataContext DataContext {
			get { return _dataContext;  }
		}

		public BaseTest() {
			SetDataContext();
		}

		public static void SetDataContext() {
			ConnectionStringSettings connectionStringSetting = ConfigurationManager.ConnectionStrings["GoConnectionString"];
			if (connectionStringSetting == null)
				throw new Exception("Expected a ConnectionString setting named 'GoConnectionString'");
			string connectionString = connectionStringSetting.ConnectionString;
			_dataContext = new GoEntitiesDataContext(connectionString);
		}

		//[ClassInitialize]
		//public static void ClassInitialize(TestContext testContext) {
		//    ConnectionStringSettings connectionStringSetting = ConfigurationManager.ConnectionStrings["GoConnectionString"];
		//    if (connectionStringSetting == null)
		//        throw new Exception("Expected a ConnectionString setting named 'GoConnectionString'");
		//    string connectionString = connectionStringSetting.ConnectionString;
		//    _dataContext = new GoEntitiesDataContext(connectionString);
		//}

		public void InitializeDatabase() {
			const string sql = @"
delete from GoError
delete from GoMove
delete from GoDeadGroup
delete from GoGame
delete from GoPlayer

--INSERTs generated by 'xsp_GenerateInserts' stored procedure written by Vyas
--Build number: 22
--Problems/Suggestions? Contact Vyas @ vyaskn@hotmail.com
--http://vyaskn.tripod.com
 
SET NOCOUNT ON
 
SET IDENTITY_INSERT [dbo].[GoPlayer] ON

 
PRINT 'Inserting values into [GoPlayer]'

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INSERT INTO [GoPlayer] ([PlayerId],[FacebookUserId],[Name],[IgsRank],[IgsUsername],[IgsPassword],[NotificationEmail])VALUES(4,'lprichar','Lee Richardson',NULL,'lprichar2','d9wZl4fBOz40QJJ19vTO3reld5zKZbprbxSUaidXbZA=','supergo@leerichardson.com')
INSERT INTO [GoPlayer] ([PlayerId],[FacebookUserId],[Name],[IgsRank],[IgsUsername],[IgsPassword],[NotificationEmail])VALUES(5,'jferner','Joe Ferner',NULL,'jferner2','xB5z9HOkDyG08B9ScMT+GCiT9o1kCTSTvzkd9xVOWH4=',NULL)
INSERT INTO [GoPlayer] ([PlayerId],[FacebookUserId],[Name],[IgsRank],[IgsUsername],[IgsPassword],[NotificationEmail])VALUES(6,'jjohnson','John Johnson',NULL,NULL,NULL,NULL)
INSERT INTO [GoPlayer] ([PlayerId],[FacebookUserId],[Name],[IgsRank],[IgsUsername],[IgsPassword],[NotificationEmail])VALUES(7,'lrichardson','Anonymous',NULL,NULL,NULL,NULL)

PRINT 'Done'
 
 
SET IDENTITY_INSERT [dbo].[GoPlayer] OFF

SET NOCOUNT OFF



SET NOCOUNT ON
 
SET IDENTITY_INSERT [dbo].[GoGame] ON
 
 
PRINT 'Inserting values into [GoGame]'

INSERT INTO [gogame] ([GameId],[BlackPlayerId],[WhitePlayerId],[DateRequestMade],[DateRequestAccepted],[RequestInitiatedByBlack],[BlackHandicap],[Komi],[Observable],[Rated],[BoardSize],[CurrentMoveNumber],[BlackCaptures],[WhiteCaptures],[DateFinished],[FinalScoreBlack],[FinalScoreWhite],[DateBlackResigned],[DateWhiteResigned],[DateBlackAcceptedDeadGroups],[DateWhiteAcceptedDeadGroups],[DateTwoPassesOccurred],[DateSubmittedToIGS])VALUES(52,5,4,'May 22 2008 12:01:49:143PM','May 22 2008 12:02:04:280PM',0,2,5.50,0,0,9,24,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)

PRINT 'Done'
 
 
SET IDENTITY_INSERT [dbo].[GoGame] OFF

SET NOCOUNT OFF




--INSERTs generated by 'xsp_GenerateInserts' stored procedure written by Vyas
--Build number: 22
--Problems/Suggestions? Contact Vyas @ vyaskn@hotmail.com
--http://vyaskn.tripod.com
 
SET NOCOUNT ON
 
SET IDENTITY_INSERT [dbo].[GoMove] ON

 
 
PRINT 'Inserting values into [GoMove]'

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(293,52,0,0,5,6,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(294,52,1,0,2,3,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(295,52,2,0,6,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(296,52,3,0,7,3,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(297,52,4,0,7,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(298,52,5,0,6,3,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(299,52,6,0,5,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(300,52,7,0,5,3,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(301,52,8,0,4,5,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(302,52,9,0,4,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(303,52,10,0,3,5,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(304,52,11,0,3,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(305,52,12,0,3,6,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(306,52,13,0,2,5,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(307,52,14,0,2,7,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(308,52,15,0,1,7,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(309,52,16,0,3,7,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(310,52,17,0,1,8,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(311,52,18,0,2,8,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(312,52,19,0,8,4,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(313,52,20,0,8,5,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(314,52,21,0,8,3,NULL)
INSERT INTO [GoMove] ([MoveId],[GameId],[MoveNumber],[Active],[X],[Y],[PreviousMoveId])VALUES(315,52,22,0,7,5,NULL)

PRINT 'Done'
 
 
SET IDENTITY_INSERT [dbo].[GoMove] OFF
SET NOCOUNT OFF
";
			_dataContext.ExecuteCommand(sql);

		}

		protected static void FinalizeGameWithCapture() {
			GoServices goServices = new GoServices();

			const string sessionKey = "blah";

			WsPlayer lprichar = goServices.GetCurrentPlayer(sessionKey, "lprichar");
			WsPlayer jferner = goServices.GetCurrentPlayer(sessionKey, "jferner");

			goServices.AddMove(52, 0, 0, false, 0, sessionKey);
			goServices.AddPass(52, true, sessionKey);
			goServices.AddPass(52, false, sessionKey);
			goServices.AddDeadGroup(52, 0, 0, 1, lprichar);
			goServices.AcceptDeadGroups(52, lprichar, sessionKey);
			goServices.FinalizeGame(52, 40, 22.5M, 1, 0, jferner, sessionKey, false);
		}
	}
}
